﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class AnswerDal
	{
		IDBHelp db = DBHelp.Create();
		Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="AnswerID">标识列ID</param>
		public bool Exists(int AnswerID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [Answer] where AnswerID=@answerid ");
			htSyn.Add("@answerid", AnswerID);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<Answer> GetModel()
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("Select [ReplyTime],[AnswerID],[QuestionID],[Description],[AdminName],[IsSee] FROM [Answer]");
			 return getAnswerBySql(strSql.ToString());
		}
		#endregion

        #region 根据问题id得到该表数据列表---郑同飞
        /// <summary>
        /// 根据问题id得到该表数据列表---郑同飞
        /// </summary>
        public IList<Answer> GetListByQuestionID(int questionID)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("Select [ReplyTime],[AnswerID],[QuestionID],[Description],[AdminName],[IsSee] FROM [Answer]");
            strSql.Append(" Where QuestionID=@questionid");
            htSyn.Add("@questionid", questionID);
            return getAnswerBySql(strSql.ToString(),htSyn);
        }
        #endregion

        #region 根据标识列查询实体
        /// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="AnswerID">标识列ID</param>
		/// <returns></returns>
		public Answer GetModel(int answerid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
            strSql.Append("select [ReplyTime],[AnswerID],[QuestionID],[Description],[AdminName],[IsSee] FROM [Answer]");
			strSql.Append(" where AnswerID=@answerid ");
			htSyn.Add("@AnswerID", answerid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			Answer answerModel = new Answer();
			if (dt.Rows.Count > 0)
			{
				answerModel.replyTime=Convert.ToDateTime(dt.Rows[0]["ReplyTime"]);
				answerModel.questionID=Convert.ToInt32(dt.Rows[0]["QuestionID"]);
				answerModel.description=dt.Rows[0]["Description"].ToString();
				answerModel.adminName=dt.Rows[0]["AdminName"].ToString();
                answerModel.isSee =Convert.ToBoolean(dt.Rows[0]["IsSee"]);
			}
			return answerModel;
		}
		#endregion  

        #region 根据问题id查询实体---郑同飞
        /// <summary>
        /// 根据问题id查询实体---郑同飞
        /// </summary>
        /// <param name="questionID">问题ID</param>
        /// <returns></returns>
        public Answer GetModelByQid(int questionID)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select [ReplyTime],[AnswerID],[QuestionID],[Description],[AdminName],[IsSee] FROM [Answer]");
            strSql.Append(" where QuestionID=@questionid ");
            htSyn.Add("@questionid", questionID);
            DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
            Answer answerModel = new Answer();
            if (dt.Rows.Count > 0)
            {
                answerModel.replyTime = Convert.ToDateTime(dt.Rows[0]["ReplyTime"]);
                answerModel.questionID = Convert.ToInt32(dt.Rows[0]["QuestionID"]);
                answerModel.description = dt.Rows[0]["Description"].ToString();
                answerModel.adminName = dt.Rows[0]["AdminName"].ToString();
                answerModel.answerID=Convert.ToInt32(dt.Rows[0]["AnswerID"]);
                answerModel.isSee = Convert.ToBoolean(dt.Rows[0]["IsSee"]);
            }
            return answerModel;
        }
        #endregion  

        #region 修改信息
        /// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="Answer">实体</param>
		/// <returns></returns>
		public bool Update(Answer answerModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [Answer] set ");
			strSql.Append("[ReplyTime]=@replyTime,");
			strSql.Append("[QuestionID]=@questionID,");
			strSql.Append("[Description]=@description,");
			strSql.Append("[AdminName]=@adminName,");
            strSql.Append("[IsSee]=@isseen");
			strSql.Append(" where AnswerID=@answerid ");
			htSyn.Add("@replytime", answerModel.replyTime);
			htSyn.Add("@answerid", answerModel.answerID);
			htSyn.Add("@questionid", answerModel.questionID);
			htSyn.Add("@description", answerModel.description);
			htSyn.Add("@adminname", answerModel.adminName);
            htSyn.Add("@issee",answerModel.isSee);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

        #region 根据问题id修改信息
        /// <summary>
        /// 根据问题id修改信息
        /// </summary>
        /// <param name="question">问题id</param>
        /// <returns></returns>
        public bool Update(int questionID)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("update [Answer] set ");           
            strSql.Append("[IsSee]=1");
            strSql.Append(" where QuestionID=@questionid");
            htSyn.Add("@questionid",questionID);           
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 根据问题id删除回答---郑同飞
        /// <summary>
        /// 根据问题id删除回答---郑同飞
        /// </summary>
        /// <param name="questions">问题id</param>
        /// <returns></returns>
        public bool DeleteID(int questionId)
        {
            strSql.Remove(0, strSql.Length);
            htSyn.Clear();
            strSql.Append("delete from answer ");           
            strSql.Append(" where ");
            strSql.Append(" QuestionID=@questionid"); 
            htSyn.Clear();
            htSyn.Add("@questionid",questionId);
            if (db.ExecuteNonQuery(strSql.ToString(),htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 删除信息
        /// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="AnswerID">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int AnswerID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [Answer] where AnswerID=@answerid");
			htSyn.Add("answerid", AnswerID);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="Answer">实体</param>
		/// <returns></returns>
		public bool Insert(Answer answerModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
            strSql.Append("insert into [Answer]([ReplyTime],[QuestionID],[Description],[adminName],[IsSee]) values (@replytime,@questionid,@description,@adminna,@issee)");
            htSyn.Add("@replytime", answerModel.replyTime);
            htSyn.Add("@questionid", answerModel.questionID);
            htSyn.Add("@description", answerModel.description);
            htSyn.Add("@adminna", answerModel.adminName);
            htSyn.Add("@issee",answerModel.isSee);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<Answer> getAnswerBySql(string strSql)
		{
			IList<Answer> list = new List<Answer>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					Answer answerModel = new Answer();
					answerModel.replyTime= Convert.ToDateTime(dr["ReplyTime"]);
					answerModel.answerID= Convert.ToInt32(dr["AnswerID"]);
					answerModel.questionID= Convert.ToInt32(dr["QuestionID"]);
					answerModel.description=dr["Description"].ToString();
					answerModel.adminName=dr["AdminName"].ToString();
                    answerModel.isSee =Convert.ToBoolean(dr["IsSee"]);
					list.Add(answerModel);
				}
			}
			return list;
		}
		#endregion

        #region 根据问题id和SQL语句获取集合
        /// <summary>
        ///根据问题id和SQL语句获取集合
        /// </summary>
        /// <param name="strSql">sql语句</param>
        public IList<Answer> getAnswerBySql(string strSql,Hashtable ht)
        {
            IList<Answer> list = new List<Answer>();
            DataTable dt = db.GetDataTable(strSql,ht);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Answer answerModel = new Answer();
                    answerModel.replyTime = Convert.ToDateTime(dr["ReplyTime"]);
                    answerModel.answerID = Convert.ToInt32(dr["AnswerID"]);
                    answerModel.questionID = Convert.ToInt32(dr["QuestionID"]);
                    answerModel.description = dr["Description"].ToString();
                    answerModel.adminName = dr["AdminName"].ToString();
                    answerModel.isSee = Convert.ToBoolean(dr["IsSee"]);
                    list.Add(answerModel);
                }
            }
            return list;
        }
        #endregion

        #region 根据回答状态问题id获得数量
        /// <summary>
        ///  根据回答状态问题id获得数量
        /// </summary>
        /// <param name="strList"></param>
        /// <param name="IsSee"></param>
        /// <returns></returns>
        public int getCount(IList<int> strList,bool IsSee)
        {
            strSql.Remove(0, strSql.Length);
            htSyn.Clear();
            strSql.Append("select count(*) as UnSee from answer where IsSee=@issee and ( ");
            foreach (int qid in strList)
            {
                strSql.AppendFormat(" questionid={0} or", qid);
            }
            strSql.Remove(strSql.Length - 1, 1);
            strSql.Remove(strSql.Length - 1, 1);
            strSql.Append(")");
            htSyn.Add("@issee",IsSee);
            object objRecordCount = null;
            int i=0;
            objRecordCount = db.ExecuteScalar(strSql.ToString(), htSyn);
            if (objRecordCount!=null)
            {
                i=Convert.ToInt32(objRecordCount);
            }
            return i;
        }
        #endregion

        #region 根据问题id获得最近的回复---郑同飞
        /// <summary>
        /// 根据问题id获得最近的回复---郑同飞
        /// </summary>
        /// <param name="qidList"></param>
        /// <returns></returns>
        public Answer getLastAnswer(IList<int> qidList)
        {           
            strSql.Remove(0, strSql.Length);
            htSyn.Clear();
            string str = "";
            foreach (int qid in qidList)
            {
                str += qid + ",";
            }
            str=str.Remove(str.Length - 1, 1);           
            strSql.AppendFormat("select [ReplyTime],[AnswerID],[AnswerID],[QuestionID],[Description],[AdminName],[IsSee] from [answer] where replytime=(select max(replytime) from answer where questionid in ({0})) and questionid in ({0})",str);
            DataTable dt = db.GetDataTable(strSql.ToString());
            Answer answerModel = new Answer();
            if (dt.Rows.Count > 0)
            {
                answerModel.replyTime = Convert.ToDateTime(dt.Rows[0]["ReplyTime"]);
                answerModel.questionID = Convert.ToInt32(dt.Rows[0]["QuestionID"]);
                answerModel.description = dt.Rows[0]["Description"].ToString();
                answerModel.adminName = dt.Rows[0]["AdminName"].ToString();
                answerModel.isSee = Convert.ToBoolean(dt.Rows[0]["IsSee"]);
                answerModel.answerID = Convert.ToInt32(dt.Rows[0]["AnswerID"]);
            }
            return answerModel;

        }
        #endregion
    }
}
